package org.ilovecode.display {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import gs.TweenLite;
	import gs.easing.*;
	import org.ilovecode.events.MenuEvent;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class BusinessMenu extends DropdownMenu {
		
		//{ region Stage elements
		
		public var contentMask:MovieClip;
		public var rentButton:MenuItem;
		public var sellButton:MenuItem;
		public var businessDescriptionButton:MenuItem;
		
		//} endregion
		
		private var timer:Timer;
		private var menuExtended:DropdownMenu;
		private var menuExtendedDeactivated:Boolean = false;
		
		public function BusinessMenu() {
			
		}
		
		public function initMenuExtended(menu:DropdownMenu):void {
			this.menuExtended = menu;
			this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
		}
		
		private function addedToStage(event:Event):void {
			this.removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
			initListeners();
			super.init(contentMask, menuExtended);
		}
		
		private function initListeners():void {
			addListenerToMenuItem(rentButton);
			//addListenerToMenuItem(sellButton);
			addListenerToMenuItem(businessDescriptionButton);
			
			sellButton.addEventListener(MouseEvent.MOUSE_OVER, sellButtonMouseOver);
			sellButton.addEventListener(MouseEvent.MOUSE_OUT, sellButtonMouseOut);
			menuExtended.addEventListener(Event.DEACTIVATE, menuExtendedDeactivate);
		}
		
		private function menuExtendedDeactivate(event:Event):void {
			menuExtendedDeactivated = true;
		}
		
		private function sellButtonMouseOver(event:MouseEvent):void {
			menuExtended.show();
			menuExtendedDeactivated = false;
		}
		
		private function sellButtonMouseOut(event:MouseEvent):void {
			timer = new Timer(500, 1);
			timer.addEventListener(TimerEvent.TIMER, timerTick);
			timer.start();
		}
		
		private function timerTick(event:TimerEvent):void {
			if (!menuExtended.isActive) {
				menuExtended.hide();
				menuExtendedDeactivated = true;
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER, timerTick);
			}
		}
		
		override public function get isActive():Boolean 
		{
			if (super.isActive || !menuExtendedDeactivated) {
				return true;
			} else {
				return false;
			}
		}
		
	}

}